<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录界面</title>
  <link rel="stylesheet" href="./css/style.css">
  <link rel="stylesheet" href="./css/body.css">
</head>

<body>
  <div class="container">
    <section id="content">
      <form>
        <h1>账号登录</h1>
        </h1>
        <div>
          <i class="close">X</i>
          <input type="text" placeholder="手机号" id="username">
        </div>
        <div>
          <i class="close">X</i>
          <input type="password" placeholder="密码" id="password">
        </div>
        <div>
          <input type="submit" value="登录" class="btn btn-primary" id="js-btn-login">
          <a href="">忘记密码?</a>
          <a href="#">注册账号</a>
        </div>
      </form>
    </section>
    <script src="../axios.js"></script>
    <script>
      // 设置统一的登录接口
      axios.defaults.baseURL = 'http://124.223.14.236:3001/api'
      //校验手机号
      let username = document.getElementById("username");
      username.addEventListener('change', mobile)
      let uMobile
      function mobile() {
        uMobile = username.value;
        if (!/^[1][3578][0-9]{9}$/.test(uMobile)) {

          alert("对不起,您的手机号无法识别，请重新输入");
          return false;
        }
        return true;
      }
      //密码长度大于6 和确认必须一致
      //获取密码框输入的值
      let password = document.getElementById("password");
      password.addEventListener('change', pass)
      let uPass
      function pass() {
        uPass = password.value;
        if (uPass.length < 6) {
          alert("对不起,您输入的密码长度太短了");
          return false;
        }
        return true
      }
      let form = document.querySelector("form");
      form.addEventListener("submit", async function (e) {
        e.preventDefault();
        if (mobile() && pass()) {
          const { data: res } = await axios.post('/user/login', {
            username: uMobile,
            password: uPass,
          })
          console.log(res);
          console.log(res.data.token);
          if (!res.success) return alert("登录失败：" + res.msg);
          localStorage.getItem('token', res.data.token)
          alert(res.msg);
          // location.href = "../01-loginBaseCode/login.html";
        } else {
          e.preventDefault()
        }

      })












    </script>
  </div>
</body>

</html>